<?php

function getPost($isVarName) {
	if(isset($_POST[$isVarName])) {
		return htmlspecialchars($_POST[$isVarName]);
	} else {
		return null;
	}
}
function getGet($isVarName) {
	if(isset($_GET[$isVarName])) {
		return htmlspecialchars($_GET[$isVarName]);
	} else {
		return null;
	}
}

function addError($isError, $isVarName, $isText) {
	$isError = $isError.$isVarName." - ".$isText."<br />";
	return $isError;
}

function mandatory($isVarName, $isInputName, &$isError) {
	if(empty($isVarName)) {
		$isError = addError($isError, $isInputName, 'povinný údaj');
	}
	return $isVarName;
}

function nvl($var1, $var2) {
	if(empty($var1)) {
		return $var2;
	} else {
		return $var1;
	}
}

function getSession($isVarName) {
	if(isset($_SESSION[$isVarName])) {
		return $_SESSION[$isVarName];
	} else {
		return null;
	}
}

function setSession($isVarName, $isVarValue) {
	$_SESSION[$isVarName] = $isVarValue;
}

function deleteSession($isVarName) {
	unset($_SESSION[$isVarName]); 
}

function init() {
	if(!isset($_SESSION['year'])) { $_SESSION['year']=DATE("Y"); }
}

function redirect($isURL) {
	header("Location: ".$isURL);
}

function getFullURL() {
	$lsPageURL = 'http';
	if ($_SERVER["HTTPS"] == "on") {$lsPageURL .= "s";}
	$lsPageURL .= "://";
	if ($_SERVER["SERVER_PORT"] != "80") {
	 $lsPageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
	} else {
	 $lsPageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
	}
	return $lsPageURL;
}

function getURL() {
	return substr($_SERVER["SCRIPT_NAME"],strrpos($_SERVER["SCRIPT_NAME"],"/")+1);
}

function isLogged() {
	$lrLogin = getSession("Login");
	if(isset($lrLogin)) {
		return TRUE;
	} else {
		return FALSE;
	}
}

function loginRequired($inLevel) {
	if(isLogged()) {
		$lrLogin = getSession("Login");
		if($inLevel > $lrLogin["level"]) {
			echo "Nedostatecna opravneni!";
			echo "LoginLevel: ".$lrLogin["level"];
		}
		
	} else {
		redirect(getURL()."?module=Auth&action=login");
	}
	//$lsLogin = getSession("Lo");
}

function generatePassword($isPassword) {
	global $lsPasswordSeed;
	return SHA1($lsPasswordSeed.$isPassword.$lsPasswordSeed);
}

/** byl odeslan formular? 
 * @ $isFormAction - name stisknuteho tlacitka
 **/
function IsFormSent($isFormAction) {
	$lsVar = getPost($isFormAction);
	if(!empty($lsVar)) {
		return TRUE;
	} else {
		return FALSE;
	}
}

/*************************************************************************************************************************************/

class systemBase {

	public $csModuleName;
	public $Model;
	public $Smarty;
	public $csTemplateAction;

	public function __construct($lsModuleName) {
		$this->csModuleName = $lsModuleName;

		/* nacteni modelu pro controller */
		require "./app/model/M".$this->csModuleName.".php";
		$lsClassName = "M".$this->csModuleName."Class";
		$this->Model = new $lsClassName;

		/* nacteni smarty */
		$this->Smarty = new Smarty;
		//$smarty->force_compile = true;
		$this->Smarty->debugging = true;
		$this->Smarty->caching = false;
		$this->Smarty->cache_lifetime = 0;

		/* zpracovani akci */
		$lsAction = getGet("action");
		if(empty($lsAction)) {
			$lsAction = "default"; // defaultni akce modulu
		}
		$lsFunctionName = "action".$lsAction;
		$this->$lsFunctionName();

		/* zobrazeni template */
		if(empty($this->csTemplateAction)) {
			$this->csTemplateAction = $lsAction;
		}
		$this->Smarty->assign("content", "T".$this->csModuleName."_".$this->csTemplateAction.".tpl");
		$this->Smarty->display("index.tpl");
	}

	public function actionDefault() {

	}

	public function viewDetail() {

	}

}

class systemDebug {
	public $debugging = false;

	public function pl($text) {
		if($this->debugging) {
			echo $text;
		}
	}
}